﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using pardusshipcalc.ShipStructure;
using System.IO;

namespace pardusshipcalc
{
    public partial class GunArmorTesting : Form
    {
        SolidBrush basicBrush = new SolidBrush(Color.FromArgb(0, 0, 200));
        SolidBrush convArmor = new SolidBrush(Color.FromArgb(79, 79, 79));
        SolidBrush orgArmor = new SolidBrush(Color.FromArgb(0, 128, 0));
        SolidBrush emArmor = new SolidBrush(Color.FromArgb(0, 255, 255));
        SolidBrush pardArmor = new SolidBrush(Color.FromArgb(164, 0, 164));
        SolidBrush avgCOEM = new SolidBrush(Color.FromArgb(153, 170, 85));
        SolidBrush avgAll = new SolidBrush(Color.FromArgb(204, 170, 85));
        SolidBrush avgOEM = new SolidBrush(Color.MediumSlateBlue);
        SolidBrush avgOEMP = new SolidBrush(Color.MediumPurple);
        SolidBrush backGnd = new SolidBrush(Color.Silver);
        SolidBrush blackBrush = new SolidBrush(Color.Black);
        SolidBrush whiteBrush = new SolidBrush(Color.White);
        SolidBrush greyBrush = new SolidBrush(Color.Gray);

        shipGunControl sgc = new shipGunControl();

        shipArmor armor = new shipArmor();

        double armorPoints = 0;
        string imagePackPath = "";
        bool start = false;
        static int[] prevIndexes = { 0, 0, 0, 0 };

        public GunArmorTesting(int aP, string IPP, int si1, int si2, int si3, int si4, int si5)
        {
            InitializeComponent();
            armorPoints = (double) aP;
            imagePackPath = IPP;
            start = true;
            armorStrengthComboBox.SelectedIndex = 0;
            armorSelectCombo.SelectedIndex = 0;
            convArmorComboBox.SelectedIndex = 0;
            orgArmorComboBox.SelectedIndex = 0;
            emArmorComboBox.SelectedIndex = 0;
            pardArmorComboBox.SelectedIndex = 0;
            cWeapSpecComboBox.SelectedIndex = 0;
            oWeapSpecComboBox.SelectedIndex = 0;
            emWeapSpecComboBox.SelectedIndex = 0;
            armor.initialiseArmor(armorSelectCombo.SelectedIndex, armorStrengthComboBox.SelectedIndex + 1);
            start = false;

            GunSelectionComboBox1.SelectedIndex = si1;
            GunSelectionComboBox2.SelectedIndex = si2;
            GunSelectionComboBox3.SelectedIndex = si3;
            GunSelectionComboBox4.SelectedIndex = si4;
            GunSelectionComboBox5.SelectedIndex = si5;
        }

        private void slaatForm_Load(object sender, EventArgs e)
        {
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void drawBasicBar(double length, Panel panel, SolidBrush brush_t)
        {
            SolidBrush brush = brush_t;
            double x = length;
            int y = 18;     

            Graphics basicBar = panel.CreateGraphics();
            basicBar.FillRectangle(greyBrush, 0, 0, 495, 20);
            basicBar.FillRectangle(brush, 0, 0, (int) x, y);
        }

        private void slaatForm_Paint(object sender, PaintEventArgs e)
        {
            if (start == false)
            {
                updateArmor();
            }
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            chartPanel1.Visible = false;
            chartPanel2.Visible = true;
            nextButton.Enabled = false;
            prevButton.Enabled = true;
            botsLabel.Visible = false;
            botBox.Visible = false;
        }

        private void prevButton_Click(object sender, EventArgs e)
        {
            chartPanel1.Visible = true;
            chartPanel2.Visible = false;
            nextButton.Enabled = true;
            prevButton.Enabled = false;
            botsLabel.Visible = true;
            botBox.Visible = true;
            updateArmor();
        }

        private void armorSelectCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (start == false)
            {
                if ((armorSelectCombo.Text == "Organic") || (armorSelectCombo.Text == "Electro-Magnetic"))
                {
                    armorStrengthComboBox.Items.Remove("1");
                    armorStrengthComboBox.Items.Remove("6");
                }
                else
                {
                    if (armorStrengthComboBox.Items.Contains("6") != true)
                    {
                        armorStrengthComboBox.Items.Add("6");
                    }
                    if (armorStrengthComboBox.Items.Contains("1") != true)
                    {
                        armorStrengthComboBox.Items.Add("1");
                    }
                    // Make sure items are in correct order when put back into the combo.
                    armorStrengthComboBox.Sorted = true;
                }
                if (armorStrengthComboBox.SelectedIndex == -1)
                {
                    armorStrengthComboBox.SelectedIndex = 0;
                }
                updateArmor();
            }
            
            
        }

        private void updateArmor()
        {
            if (armor != null)
            {
                armor.initialiseArmor(armorSelectCombo.SelectedIndex, armorStrengthComboBox.SelectedIndex + 1);
                armorPictureBox.ImageLocation = Path.Combine(imagePackPath, armor.getEquipmentPath());

                if (armor.isPardusArm() == true)
                {
                    armorPictureBox.BackColor = Color.Purple;
                }
                else
                {
                    armorPictureBox.BackColor = Color.Transparent;
                }
            }
            calcBots();
            reDrawInfoBars();
        }

        private void calcBots()
        {
            int aStrength = armorStrengthComboBox.SelectedIndex +1;
            if (aStrength > 0)
            {
                botsLabel.Text = " will repair " + (180 / aStrength).ToString("N0") + " points of armour per ton. " + (armorPoints / (180 / aStrength)).ToString("N1") + " tons required to repair armour fully.";
                botsLabel.Visible = true;
                botBox.Visible = true;
            }
            else
            {
                botsLabel.Visible = false;
                botBox.Visible = false;
            }

        }

        private void reDrawInfoBars()
        {
            if (armorStrengthComboBox.SelectedIndex != -1)
            {
                Guns shipGuns = new Guns();
                double str = double.Parse(armorStrengthComboBox.Text);
                double effArmor = (armorPoints * str);

                double Cefficiency = (effArmor / ((shipGuns.determineGunEfficiency(0,true, armorSelectCombo.SelectedIndex ,null) / 100)));
                double Oefficiency = (effArmor / ((shipGuns.determineGunEfficiency(1, true, armorSelectCombo.SelectedIndex, null) /100)));
                double EMefficiency = (effArmor / ((shipGuns.determineGunEfficiency(2, true, armorSelectCombo.SelectedIndex, null) / 100)));
                double Pefficiency = (effArmor / ((shipGuns.determineGunEfficiency(3, true, armorSelectCombo.SelectedIndex, null) /100)));
                double COEMefficiency = ((Cefficiency + Oefficiency + EMefficiency) / 3);
                double Allefficiency = ((Cefficiency + Oefficiency + EMefficiency + Pefficiency) / 4);
                double OEMefficiency = ((Oefficiency + EMefficiency) / 2);
                double OEMPefficiency = ((Oefficiency + EMefficiency + Pefficiency) / 3);
                
                double scaling = (armorPoints / 55);
                
                actualArmorLabel.Text = "Actual: " + armorPoints.ToString("N0");
                effectiveArmorLabel.Text = "Effective: " + (effArmor).ToString("N0");
                effectiveVsCLabel.Text = "Vs. Conv: " + (Cefficiency).ToString("N0");
                effectiveVsOLabel.Text = "Vs. Org: " + (Oefficiency).ToString("N0");
                effectiveVsEMLabel.Text = "Vs. EM: " + (EMefficiency).ToString("N0");
                effectiveVsPLabel.Text = "Vs. Pardus: " + (Pefficiency).ToString("N0");
                effectiveVsCOEMLabel.Text = "Vs. COEM: " + (COEMefficiency).ToString("N0");
                effectiveVsAllLabel.Text = "Vs. All: " + (Allefficiency).ToString("N0");
                effectiveVsOEMLabel.Text = "Vs. OEM: " + OEMefficiency.ToString("N0");
                effectiveVsOEMPLabel.Text = "Vs. OEMP: " + OEMPefficiency.ToString("N0");

                drawBasicBar(armorPoints / scaling, graphPanel1, basicBrush);
                drawBasicBar(effArmor / scaling, effectiveArmorPanel, basicBrush);
                drawBasicBar(Cefficiency/scaling, effectiveVsCPanel, convArmor);
                drawBasicBar(Oefficiency/scaling, effectiveVsOPanel, orgArmor);
                drawBasicBar(EMefficiency / scaling, effectiveVsEMPanel, emArmor);
                drawBasicBar(Pefficiency / scaling, effectiveVsPPanel, pardArmor);
                drawBasicBar(COEMefficiency / scaling, effectiveVsCOEMPanel, avgCOEM);
                drawBasicBar(Allefficiency / scaling, effectiveVsAllPanel, avgAll);
                drawBasicBar(OEMefficiency / scaling, effectiveVsOEMPanel, avgOEM);
                drawBasicBar(OEMPefficiency / scaling, effectiveVsOEMPPanel, avgOEMP);
            }
        }

        private void armorStrengthComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (start == false)
            {
                updateArmor();
            }
        }

        private void pvpCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (pvpCheckBox.Checked == true)
            {
                prevIndexes[0] = convArmorComboBox.SelectedIndex;
                prevIndexes[1] = orgArmorComboBox.SelectedIndex;
                prevIndexes[2] = emArmorComboBox.SelectedIndex;
                prevIndexes[3] = pardArmorComboBox.SelectedIndex;

                convArmorComboBox.SelectedIndex = 5;
                orgArmorComboBox.SelectedIndex = 3;
                emArmorComboBox.SelectedIndex = 3;
                pardArmorComboBox.SelectedIndex = 5;
            }
            else
            {
                convArmorComboBox.SelectedIndex = prevIndexes[0];
                orgArmorComboBox.SelectedIndex = prevIndexes[1];
                emArmorComboBox.SelectedIndex = prevIndexes[2];
                pardArmorComboBox.SelectedIndex = prevIndexes[3];
            }
        }

        private void convArmorComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            shipArmor sa = new shipArmor();
            sa.initialiseArmor(0, convArmorComboBox.SelectedIndex + 1);
            convArmorPictureBox.ImageLocation = Path.Combine(imagePackPath, sa.getEquipmentPath());
            updateAllGuns();
        }

        private void orgArmorComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            shipArmor sa = new shipArmor();
            sa.initialiseArmor(1, orgArmorComboBox.SelectedIndex + 1);
            orgArmorPictureBox.ImageLocation = Path.Combine(imagePackPath, sa.getEquipmentPath());
            updateAllGuns();
        }

        private void emArmorComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            shipArmor sa = new shipArmor();
            sa.initialiseArmor(2, emArmorComboBox.SelectedIndex + 1);
            emArmorPictureBox.ImageLocation = Path.Combine(imagePackPath, sa.getEquipmentPath());
            updateAllGuns();
        }

        private void pardArmorComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            shipArmor sa = new shipArmor();
            sa.initialiseArmor(3, pardArmorComboBox.SelectedIndex + 1);
            pardusArmorPictureBox.ImageLocation = Path.Combine(imagePackPath, sa.getEquipmentPath());
            updateAllGuns();

            if (pardArmorComboBox.SelectedIndex >= 0)
            {
                pardusArmorPictureBox.BackColor = Color.Purple;
            }
        }

        private void GunSelectionComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            sgc.setGun(GunSelectionComboBox1.SelectedIndex, 0);
            updateGun(0);
        }
        private void GunSelectionComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            sgc.setGun(GunSelectionComboBox2.SelectedIndex, 1);
            updateGun(1);
        }

        private void GunSelectionComboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            sgc.setGun(GunSelectionComboBox3.SelectedIndex, 2);
            updateGun(2);
        }

        private void GunSelectionComboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            sgc.setGun(GunSelectionComboBox4.SelectedIndex, 3);
            updateGun(3);
        }

        private void GunSelectionComboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            sgc.setGun(GunSelectionComboBox5.SelectedIndex, 4);
            updateGun(4);
        }

        private void updateAllGuns()
        {
            updateGun(0);
            updateGun(1);
            updateGun(2);
            updateGun(3);
            updateGun(4);
        }

        private void updateGun(int number)
        {
            Label[] baseDamage = { baseGun1Label, baseGun2Label, baseGun3label, baseGun4Label, baseGun5Label };
            Label[] convDamage = { cDamageLabel1, cDamageLabel2, cDamageLabel3, cDamageLabel4, cDamageLabel5 };
            Label[] orgDamage = { oDamageLabel1, oDamageLabel2, oDamageLabel3, oDamageLabel4, oDamageLabel5 };
            Label[] emDamage = { emDamageLabel1, emDamageLabel2, emDamageLabel3, emDamageLabel4, emDamageLabel5 };
            Label[] pDamage = { pDamageLabel1, pDamageLabel2, pDamageLabel3, pDamageLabel4, pDamageLabel5 };
            Label[] avgDamage = { avgDamageLabel1, avgDamageLabel2, avgDamageLabel3, avgDamageLabel4, avgDamageLabel5 };

            Guns sg = new Guns();

            double damage = sgc.getGunDamage(number);
            double fireRate = sgc.getGunFireRate(number);           
            double convDamage_v;
            double oDamage_v;
            double emDamage_v;
            double pDamage_v;
            double avgDamage_v =0;

            int cStr = convArmorComboBox.SelectedIndex + 1;
            int oStr = orgArmorComboBox.SelectedIndex + 2;
            int emStr = emArmorComboBox.SelectedIndex + 2;
            int pStr = pardArmorComboBox.SelectedIndex + 1;

            damage = damage * fireRate;

            baseDamage[number].Text = damage.ToString("N0");

            convDamage_v = sg.determineGunEfficiency(sgc.getGunType(number), true, 0, null);            
            convDamage_v = (((damage * convDamage_v) / 100) / cStr);
            convDamage_v = ((convDamage_v / 100) * (100 + (cWeapSpecComboBox.SelectedIndex * 5)));
            convDamage_v = Math.Round(convDamage_v, 0);
            convDamage[number].Text = convDamage_v.ToString("N0");

            oDamage_v = sg.determineGunEfficiency(sgc.getGunType(number), true, 1, null);
            oDamage_v = (((damage * oDamage_v)/100) / oStr);
            oDamage_v = ((oDamage_v / 100) * (100 + (oWeapSpecComboBox.SelectedIndex * 5)));
            oDamage_v = Math.Round(oDamage_v, 0);
            orgDamage[number].Text = oDamage_v.ToString("N0");

            emDamage_v = sg.determineGunEfficiency(sgc.getGunType(number), true, 2, null);
            emDamage_v = (((damage * emDamage_v)/ 100) / emStr);
            emDamage_v = ((emDamage_v / 100) * (100 + (emWeapSpecComboBox.SelectedIndex * 5)));
            emDamage_v = Math.Round(emDamage_v, 0);
            emDamage[number].Text = emDamage_v.ToString("N0");

            pDamage_v = sg.determineGunEfficiency(sgc.getGunType(number), true, 3, null);
            pDamage_v = (((damage * pDamage_v) /100) / pStr);
            pDamage[number].Text = pDamage_v.ToString("N0");

            //AVG:
           avgDamage_v = (convDamage_v + oDamage_v + emDamage_v + pDamage_v) /4;
           
            avgDamage[number].Text = (avgDamage_v).ToString("N1");
                    


            
        }

        private void cWeapSpecComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateAllGuns();
        }

        private void oWeapSpecComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateAllGuns();
        }

        private void emWeapSpecComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateAllGuns();
        }


   }
}
